package com.controller;

import com.dto.MeetingDTO;
import com.dto.MeetingViewDTO;
import com.dto.MyMeetingDTO;
import com.pojo.Attendee;
import com.pojo.Meeting;
import com.pojo.User;
import com.service.AttendeeService;
import com.service.MeetingService;
import com.utils.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MyMeetingController {

    @Autowired
    private MeetingService meetingService;
    @Autowired
    private AttendeeService attendeeService;

    @RequestMapping("/meeting/noticelist.html")
    public String list(Model model,
                       @RequestParam(value="pageIndex",required=false) Integer pageIndex,
                       HttpSession session){


        System.out.println(pageIndex);
        int pageSize=5;
        if(pageIndex==null||pageIndex<1) {
            pageIndex=1;
        }

        Integer uid = ((User)session.getAttribute("userSession")).getId();

        // 符合条件的总数量
        int totalCount=meetingService.getMeetingCountByUid(uid);
        System.out.println("totalCount="+totalCount);

        // 分页控制
        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(pageIndex);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount=pages.getTotalPageCount();
        System.out.println(totalPageCount);
        if(pageIndex>totalPageCount) {
            pageIndex=totalPageCount;
        }
        System.out.println(pageIndex);
        // 获取用户列表
        List<MyMeetingDTO> MyMeetingDTOList=meetingService.getMeetingListByUid(uid,(pageIndex-1)*pageSize, pageSize);
        model.addAttribute("meetList",MyMeetingDTOList);
        MyMeetingDTOList.forEach(System.out::println);



        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("pageIndex",pageIndex);

        return "/mymeeting/list";

    }

    @GetMapping(value="/mymeeting/view/{mid}/{aid}")
    public String viewMeeting(@PathVariable("mid") Integer mid,
                              @PathVariable("aid") Integer aid,
                              Model model,
                              HttpSession session) {
        MeetingViewDTO meetingViewDTO = meetingService.getMeetingById(mid);
        Integer uid = ((User)session.getAttribute("userSession")).getId();
        Attendee attendee = new Attendee();
        attendee.setId(aid);
        attendee.setUserid(uid);
        attendee.setReadstatus(true);
        attendeeService.updateByPrimaryKeySelective(attendee);
        model.addAttribute("meeting", meetingViewDTO);
        return "/meeting/view";
    }

    @RequestMapping(value="/mymeeting/updateCheckStatus.html/{aid}")
    public String doUpdateMeetCheckStatus(@PathVariable Integer aid,
                                          HttpSession session) {
        Integer uid = ((User)session.getAttribute("userSession")).getId();
        Attendee attendee = new Attendee();
        attendee.setId(aid);
        attendee.setUserid(uid);
        attendee.setCheckstatus(true);
        attendeeService.updateByPrimaryKeySelective(attendee);

        return "redirect:/meeting/noticelist.html";
    }
}
